package com.seirsoft.framework.payload;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


public class PayloadColumnCollection
{
	private int m_columnCount;
    private int m_intCount;
	private List<PayloadColumn> m_columns;
    private Hashtable<String, Integer> m_columnNames;
    
    public PayloadColumnCollection()
    {
    	this.m_columnCount = 0;
        this.m_intCount = 0;
        this.m_columns = new ArrayList<PayloadColumn>();
    	this.m_columnNames = new Hashtable<String, Integer>();   	
    }
    
    public int getCount()
    {
    	return this.m_columnCount;
    }
    
    public int getIntCount()
    {
    	return this.m_intCount;
    }
    
    public PayloadColumn get(int index)
    {
    	return this.m_columns.get(index);
    }
    
    // Methods
    public void addColumn(PayloadColumn column)
    {
    	this.m_columns.add(column);
    	
    	this.m_columnNames.put(column.getName(), this.m_columnCount);
    	this.m_columnCount++;
    	
        if (column.getIsValue())
        {
            column.setOffset(this.m_intCount);
            this.m_intCount++;
        }
    }
    
    

    /**
     *  Factory class that keeps the parse method
     */
    public static class Factory {
        /**
         * static method to create the object
         * Precondition:  If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
         *                If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
         * Postcondition: If this object is an element, the reader is positioned at its end element
         *                If this object is a complex type, the reader is positioned at the end element of its outer element
         */
        public static void parse(
            javax.xml.stream.XMLStreamReader reader,
            PayloadTable dataTable) throws java.lang.Exception {
        	
        	PayloadColumnCollection object = dataTable.getColumns();
        	
        	/*
            reader.ReadStartElement();
            if (reader.IsStartElement("columns"))
            {
                reader.ReadStartElement();
                for (int i = 0; reader.IsStartElement("c"); i++)
                {
                    RowSetColumn item = new RowSetColumn();
                    ((IXmlSerializable) item).ReadXml(reader);
                    list2.Add(item);
                    this.m_columnNames.Add(item.Name, i);
                }
                reader.ReadEndElement();
            }
            
            this.m_columnNames = new Hashtable<String, Integer>();

            this.m_columns = list2.ToArray();
            this.InitColumns();
                	 * */
        	
        	 try {
            	
                while (!reader.isStartElement() && !reader.isEndElement())
                    reader.next();

            	// get Next Element: <c> or </columns>
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());

                while (reader.isStartElement())
                {   				
            		if(reader.getLocalName().equals("c"))
            		{
                        PayloadColumn column = PayloadColumn.Factory.parse(reader);
        				object.addColumn(column);
            		}
            		else
            		{
            			// </Unkown>
	                    do{
	                    	reader.next();
	                    } while (!reader.isStartElement() && !reader.isEndElement());
            		}
            		
            		// get next Element: <c> or </columns>
            		do{
                    	reader.next();
                    } while (!reader.isStartElement() && !reader.isEndElement());
                }

            } catch (javax.xml.stream.XMLStreamException e) {
                throw new java.lang.Exception(e);
            }
        }
    } //end of factory class
}
